package com.xiebishe.mylibrary.tools;

import android.os.IBinder;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.xiebishe.mylibrary.base.MyApplication;

import static android.content.Context.INPUT_METHOD_SERVICE;

public class SoftInputUtils {

    /**
     * 隐藏软键盘
     *
     * @param windowToken
     */
    public static void hideSoftInput(IBinder windowToken) {
        InputMethodManager imm = ((InputMethodManager) MyApplication.getInstance().getApplicationContext().getSystemService(INPUT_METHOD_SERVICE));
        imm.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS);//InputMethodManager.HIDE_NOT_ALWAYS
    }

    /**
     * 打开软键盘
     *
     * @param view 接受输入的view
     */
    public static void showSoftInput(View view) {
        InputMethodManager imm = ((InputMethodManager) MyApplication.getInstance().getSystemService(INPUT_METHOD_SERVICE));
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }


    /**
     * EditText强行要求输入
     *
     * @param editText the edit text
     */
    public static void requestInput(EditText editText) {
        if (editText != null) {
            requestFocus(editText);
            editText.setSelection(editText.getText().length());
            SoftInputUtils.showSoftInput(editText);
        }
    }
    public static void requestFocus(View view) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
    }
    public static KeyboardStatusDetector detectSoftInput(@NonNull AppCompatActivity activity, KeyboardStatusDetector.KeyboardVisibilityListener listener) {
        return new KeyboardStatusDetector().registerActivity(activity).setListener(listener);
    }


}

